package com.example.bilibili_fake.ui.home

import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
import com.bumptech.glide.Glide
import com.bumptech.glide.load.model.GlideUrl
import com.bumptech.glide.load.model.LazyHeaders
import com.example.bannerapi.model.BannerItem
import com.example.bilibili_fake.R
import com.example.bilibili_fake.utils.LogUtil
import com.youth.banner.adapter.BannerAdapter
import com.youth.banner.holder.BannerImageHolder

class BannerAdapter(private val mDatas: List<BannerItem>) :
    BannerAdapter<BannerItem, BannerAdapter.BannerViewHolder>(mDatas) {

    override fun onCreateHolder(parent: ViewGroup, viewType: Int): BannerViewHolder {
        val view = LayoutInflater.from(parent.context)
            .inflate(R.layout.item_banner, parent, false)
        return BannerViewHolder(view)
    }

    override fun onBindView(holder: BannerViewHolder, data: BannerItem, position: Int, size: Int) {
        holder.title.text = data.title
        val glideUrl = GlideUrl(
            data.pic, LazyHeaders.Builder()
                .addHeader(
                    "User-Agent",
                    "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36"
                )
                .build()
        )
        Glide.with(holder.itemView)
            .load(glideUrl)
            .into(holder.image)
        LogUtil.d("BannerAdapter", "Banner image URL: $glideUrl")
    }

    inner class BannerViewHolder(view: View) : RecyclerView.ViewHolder(view) {
        val image: ImageView = view.findViewById(R.id.bannerImage)
        val title: TextView = view.findViewById(R.id.bannerText)
    }


}